<?php
namespace Backend\Controllers;

use Models\SettingProfile;
use Models\User;
use Models\UserProfile;

class MyinfoController extends BaseController
{
    public function indexAction()
    {
        $Cuser = $this->Cuser;
        $user = User::findFirstByuid($Cuser->uid);
        $userProfile = UserProfile::findFirstByUid($Cuser->uid);
        if ($this->request->isPost() == true) {
            if ($this->security->checkToken()) {
                if ($user->update()) {
                    if (!$userProfile) {
                        $userProfile = new UserProfile();
                        $userProfile->uid = $user->uid;
                        $userProfile->create($this->request->getPost());
                        return $this->response->redirect("admin/myinfo");
                    } else {
                        if ($userProfile->update($this->request->getPost())) {
                            return $this->response->redirect("admin/myinfo");
                        } else {
                            return $this->flash->warning("编辑失败");
                        }
                    }
                } else {
                    return $this->flash->warning("编辑失败");
                }
            } else {
                $this->flash->warning("Access Denied!");
            }
        } else {
            $this->view->setVar("user", $Cuser);
            $profile = array();
            foreach (SettingProfile::find() as $value) {
                $profile[$value->title] = $value->variable;
            }
            $this->view->setVar("profile", $profile);
            $this->view->setVar("userprofile", (array)$userProfile);
        }
    }

//    public function rechargeAction()
//    {
//        $Cuser = $this->Cuser;
//        $user = User::findFirst($Cuser->uid);
//        $this->view->setVar("user", $user);
//        if ($this->request->getPost() == true) {
//            if ($this->request->getPost("sum") <= 0) return $this->flash->warning("请输入充值金额");
//            $user->sum = $user->sum + $this->request->getPost("sum");
//            if ($user->update()) {
//                return $this->flash->warning("充值成功");
//            } else {
//                return $this->flash->warning("编辑失败");
//            }
//
//        }
//    }


}